import axios from 'axios'
import Vue from'vue'
import { Toast } from 'vant';

Vue.use(Toast);
const service = axios.create({
    baseURL: process.env.API_ROOT, // api的base_url
    // baseURL:'/tolang/api/', // api的base_url
    timeout: 500000 // 请求超时时间
  })
// request拦截器
service.interceptors.request.use(config => {
    const toast = Toast.loading({
        duration: 0,       // 持续展示 toast
        loadingType: 'circular',
        forbidClick: true, // 禁用背景点击  
      });
    return config
  }, error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  })

  service.interceptors.response.use(response => {
    Toast.clear();
    return response
  }, error => {
//        console.log('2'+error)
switch (error.response.status) {
    case 400:
      if (error.response.data) {
        return error.response.data
      }
      break;
    case 500:
        // router.replace({
        //   path: '/404',
        // })
        Toast('服务端异常，请联系客服。');
        return error.response.data
        break;
    }
    
    Toast.clear();
    return Promise.reject(error)
  })
  export default service